package org.sith.taskrunner.ejb.interceptors;

import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;
import java.util.concurrent.Semaphore;


public class SynchronizationInterceptor {
    private static final Semaphore semaphore = new Semaphore(1, true);

    @AroundInvoke
    public Object doMethod(InvocationContext context) throws Exception {
        try {
            semaphore.acquire();
            return context.proceed();
        } catch (Exception e) {
            throw e;
        } finally {
            semaphore.release();
        }
    }
}
